<template>
    <el-dialog
        v-dialogDrag
        :title="dialogType == 1 ? '新增' : '修改'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        @close="handleClose"
        :center="true"
    >
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
            <el-form-item label="标题">
                <el-input v-model="dataForm.name" placeholder="请输入标题"  clearable></el-input>
            </el-form-item>
            <el-form-item label="banner图片">
                <upload :img="dataForm.imgUrl" @update="update" />
            </el-form-item>
            <el-form-item label="跳转URL" prop="goUrl">
                <el-input v-model="dataForm.goUrl" placeholder="请输入跳转URL"  clearable></el-input>
            </el-form-item>
            <el-form-item label="排序号" prop="orderno">
                <el-input-number v-model="dataForm.orderno" controls-position="right" :min="1" label="排序号"> </el-input-number>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';
import upload from '../../components/upLoad/index';
export default {
    components: {
        upload
    },
    data() {
        return {
            visible: false,
            dataForm: {
                goUrl: '',
                name: '',
                imgUrl: '',
                orderno: ''
            },
            appId: '',
            dataRule: {
                name: [
                    {
                        required: true,
                        message: '名称不能为空',
                        trigger: 'blur'
                    }
                ],
                imgUrl: [
                    {
                        required: true,
                        message: '请上传图片',
                        trigger: 'blur'
                    }
                ]
            },
            dialogType: 1
        };
    },
    methods: {
        handleClose() {
            this.dataForm = {
                goUrl: '',
                name: '',
                imgUrl: '',
                orderno: ''
            };
            this.$refs['dataForm'].resetFields();
        },
        getList(appId) {
            API.role
                .getRoleUserList({
                    appId: appId
                })
                .then(res => {
                    console.log(res);
                    this.typeOptions = res.data;
                });
        },
        update(img) {
            this.dataForm.imgUrl = img;
        },
        init(item, type) {
            this.visible = true;

            this.dialogType = type;
            if (item && type == 2) {
                this.dataForm = item;
            }
        },

        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate(valid => {
                if (valid) {
                    var tick = this.dialogType == 1 ? API.banner.addBanner(this.dataForm) : API.banner.editBanner(this.dataForm);
                    tick.then(data => {
                        if (data.code == '0000') {
                            this.$message({
                                message: `${this.dialogType == 2 ? '修改成功' : '新增成功'}`,
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit('refreshDataList');
                                }
                            });
                        }
                    });
                }
            });
        }
    }
};
</script>